//
//  HotVC.m
//  echo
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Danny. All rights reserved.
//

#import "HotVC.h"
#import "NetworkingHandler.h"
#import "ModelForHotVCScrollView.h"
#import "CellForHotTableView.h"
#import "ModelForHotVCTableViewCell.h"
#import "CellForHotVCTableViewSectionOneAndTwo.h"
#import "ModelForHotVCTableViewCellSectionOneAndTwo.h"
#import "MusicPlayerVC.h"
#import <DKNightVersion.h>
#import "RecommendVC.h"

@interface HotVC ()<UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrForCell;
@property (nonatomic, retain)NSMutableArray *arrForTableViewCell;
@property (nonatomic, retain)NSMutableArray *arrForCellOfSectionOne;
@property (nonatomic, retain)NSMutableArray *arrForCellOfSectionTwo;
@end

@implementation HotVC

- (void)dealloc{
    [_arrForCell release];
    [_arrForTableViewCell release];
    [_arrForCellOfSectionOne release];
    [_arrForCellOfSectionTwo release];
    self.tableView.delegate = nil;
    [_tableView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postModelForHotVCLeft" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postModelForHotVCRight" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self handleDataForTableViewCell];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForHotVCLeft" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForHotVCTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelModelForHotVC:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForHotVCRight" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForHotVCTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelModelForHotVC:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }];
    
    self.tabBarController.delegate = self;
    
}

- (void)handleDataForTableViewCell{
    [NetworkingHandler networkingPostWithUrl:@"http://echosystem.kibey.com/hot/sounds" body:@"v=9&period=daily" dicForValue:@{@"x-v":@"20160209",@"Content-Type":@"application/x-www-form-urlencoded",@"x-c":@"3"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *arr = [dic objectForKey:@"data"];
        self.arrForCell = [NSMutableArray array];
        for (NSDictionary *tempDic in arr) {
            ModelForHotVCTableViewCell *model = [[ModelForHotVCTableViewCell alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrForCell addObject:model];
            [model release];
        }
        self.arrForTableViewCell = [NSMutableArray array];
        for (int i = 0; i < self.arrForCell.count; i++) {
            if (i%2 == 0) {
                NSMutableArray *arrForCell2 = [NSMutableArray array];
                [arrForCell2 addObject:self.arrForCell[i]];
                [arrForCell2 addObject:self.arrForCell[i+1]];
                [self.arrForTableViewCell addObject:arrForCell2];
            }
        }
        [self handleDataForTableViewCellSectionOne];
    }];
}

- (void)handleDataForTableViewCellSectionOne{
    [NetworkingHandler networkingPostWithUrl:@"http://echosystem.kibey.com/hot/sounds" body:@"v=9&period=weekly" dicForValue:@{@"x-v":@"20160209",@"Content-Type":@"application/x-www-form-urlencoded",@"x-c":@"3"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *arr = [dic objectForKey:@"data"];
        self.arrForCellOfSectionOne = [NSMutableArray array];
        for (NSDictionary *tempDic in arr) {
            ModelForHotVCTableViewCellSectionOneAndTwo *model = [[ModelForHotVCTableViewCellSectionOneAndTwo alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrForCellOfSectionOne addObject:model];
            [model release];
        }
        [self handleDataForTableViewCellSectionTwo];
    }];
}

- (void)handleDataForTableViewCellSectionTwo{
    [NetworkingHandler networkingPostWithUrl:@"http://echosystem.kibey.com/hot/sounds" body:@"v=9&period=monthly" dicForValue:@{@"x-v":@"20160209",@"Content-Type":@"application/x-www-form-urlencoded",@"x-c":@"3"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *arr = [dic objectForKey:@"data"];
        self.arrForCellOfSectionTwo = [NSMutableArray array];
        for (NSDictionary *tempDic in arr) {
            ModelForHotVCTableViewCellSectionOneAndTwo *model = [[ModelForHotVCTableViewCellSectionOneAndTwo alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrForCellOfSectionTwo addObject:model];
            [model release];
        }
        [self handleDataForScrollViewImage];
    }];
}

- (void)handleDataForScrollViewImage{
    [NetworkingHandler networkingGetWithUrl:@"http://echosystem.kibey.com/index/banner?position=1" dicForValue:@{@"User-Agent":@"echo ios 3.7.3 20160209;(iPhone,iPhone OS9.2.1);IDFA F37139C2-85EF-45C8-B64D-2D16D2772A96 Paros/3.2.13"}  completeHandle:^(id result) {
        
        NSArray *arr = [result objectForKey:@"result"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ModelForHotVCScrollView *model = [[ModelForHotVCScrollView alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            [model release];
        }
        
        [self.arr addObject:[self.arr firstObject]];
        [self.arr insertObject:[self.arr objectAtIndex:self.arr.count - 2] atIndex:0];
        
        [self createScrollView];
        [self createTableView];
    }];

}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 93) style:UITableViewStyleGrouped];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[CellForHotTableView class] forCellReuseIdentifier:@"poolForCellOfHotVCSectionZero"];
    [self.tableView registerClass:[CellForHotVCTableViewSectionOneAndTwo class] forCellReuseIdentifier:@"poolForCellOfHotVCSectionOneAndTwo"];
    self.tableView.tableHeaderView = self.backView;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        
        return 5;
    }else{
        
        return 10;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CellForHotTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellOfHotVCSectionZero"];
        NSArray *arr = [self.arrForTableViewCell objectAtIndex:indexPath.row];
        [cell passModelForCell:arr];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
        
    }else if (indexPath.section == 1){
        CellForHotVCTableViewSectionOneAndTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellOfHotVCSectionOneAndTwo"];
        ModelForHotVCTableViewCellSectionOneAndTwo *model = [self.arrForCellOfSectionOne objectAtIndex:indexPath.row];
        [cell passDataOfHotVCTableViewCellSectionOneAndTwo:model];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else{
        CellForHotVCTableViewSectionOneAndTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellOfHotVCSectionOneAndTwo"];
        ModelForHotVCTableViewCellSectionOneAndTwo *model = [self.arrForCellOfSectionTwo objectAtIndex:indexPath.row];
        [cell passDataOfHotVCTableViewCellSectionOneAndTwo:model];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        UIView *viewForHeader1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHT)];
        viewForHeader1.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20 * HEIGHT)];
        label1.text = @"     今日最热";
        label1.textColor = [UIColor greenColor];
        [viewForHeader1 addSubview:label1];
        [label1 release];
        
        return viewForHeader1;
    }else if (section == 1){
        
        UIView *viewForHeader2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45 * HEIGHT)];
        viewForHeader2.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 25 * HEIGHT)];
        label2.text = @"     本周最热";
        label2.textColor = [UIColor greenColor];
        [viewForHeader2 addSubview:label2];
        [label2 release];
        
        return viewForHeader2;
    }else{
        UIView *viewForHeader3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45 * HEIGHT)];
        viewForHeader3.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 25 * HEIGHT)];
        label3.text = @"     本月最热";
        label3.textColor = [UIColor greenColor];
        [viewForHeader3 addSubview:label3];
        [label3 release];
        
        return viewForHeader3;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 40;
    }else{
        return 45;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 257 * HEIGHT;
    }else{
        return 82.8 * HEIGHT;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForHotVCTableViewCellSectionOneAndTwo *model = [self.arrForCellOfSectionOne objectAtIndex:indexPath.row];
        [vc passModelForHotVCTableViewCellSectionOneAndTwo:model];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 2) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForHotVCTableViewCellSectionOneAndTwo *model = [self.arrForCellOfSectionTwo objectAtIndex:indexPath.row];
        [vc passModelForHotVCTableViewCellSectionOneAndTwo:model];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    if (tabBarController.selectedIndex == 2) {
        
        if(viewController == [tabBarController.viewControllers objectAtIndex:2])
        {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpForThird" object:nil userInfo:nil];
            return YES;
        }
    }
    
    
    
    return YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationBarHidden = NO;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:18 / 256.f green:29 / 256.f blue:50 / 256.f alpha:1]);
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
